vm = new Vue({
    el: '#app',  //选中整个main
    data: {
        hotGoodsList: {},
        goods: JSON.parse(sessionStorage.getItem('goods')),
        tatalMoney: 0,
        count: 1,
        order:{},
        favorite:'',
        types: {},
        goodsName: ''
    },
    methods: {
        exitLogin:function(){
            axios.get("user/exitLogin").then(rs=>{
                sessionStorage.removeItem("user");
                sessionStorage.removeItem("showNum");
                sessionStorage.removeItem("typeId");
                layer.msg("退出登录成功！");
                location.href="index.html";
            });
        },
        //搜索，模糊查询
        search: function () {
            sessionStorage.setItem("typeId", 0);
            sessionStorage.setItem("goodsName", this.goodsName);
            location.href = 'productList.html';
        },
        //列表转换
        toGoodsListByType: function (typeId) {
            sessionStorage.setItem("typeId", typeId);
            sessionStorage.removeItem("goodsName");
            location.href = 'productList.html';
        },
        //获取商品列表
        getAllGoodsType: function () {
            axios.get('type/getAllGoodsType').then(rs => {
                this.types = rs.data.obj;
            });
        },
        getGoods: function () {
            axios.get("goods/getHotGoods").then(rs => {
                this.hotGoodsList = rs.data.obj;
            })
        },
        add: function () {
            this.count++;
            this.tatalMoney=this.goods.price*this.count
            $(".totalMoney").html("$" + this.tatalMoney);
        },
        reduce: function () {
            if (this.count > 1) {
                this.count--;
                this.tatalMoney=this.goods.price*this.count
                $(".totalMoney").html("$" + this.tatalMoney);
            }
        },
        //商品详情
        toDetail:function (goods) {
            // event.preventDefault();
            sessionStorage.setItem('goods',JSON.stringify(goods))
            location.href='goodsDetail.html';
        },
        //添加购物车
        addShoppingCart:function () {
            this.order.goodsId=this.goods.id;
            this.order.goodsCount=this.count;
            this.order.totalMoney=this.tatalMoney;
            this.order.userId=JSON.parse(sessionStorage.getItem('user')).id;
            axios.post('order/addShoppingCart',this.order).then(rs=>{
                location.href='car.html';
                layer.msg("添加购物车成功")
            })
        },
        //收藏
        favoriteGoods:function () {
            axios.post(`sysUserGoods/favoriteGoods/${JSON.parse(sessionStorage.getItem('user')).id}/${this.goods.id}`).then(rs=>{
                if (rs.data.success){
                    this.favorite='w3ls-cart-liked'
                }else
                    this.favorite='w3ls-cart-like'
                // alert(rs.data.msg);
                layer.msg(rs.data.msg);
            })
        },
        //是否是已经收藏
        isFavorate:function () {
            axios.get(`sysUserGoods/isFavorate/${JSON.parse(sessionStorage.getItem('user')).id}/${this.goods.id}`).then(rs=>{
                // alert(rs.data.obj)
                if (rs.data.obj){
                    this.favorite='w3ls-cart-liked'
                }else {
                    this.favorite='w3ls-cart-like'
                }
            })
        },
        getUserInfo:function () {
            // debugger;
            let user = sessionStorage.getItem("user");
            if (user==''||user==undefined||user==null) {
                axios.get('user/getUserInfo').then(rs => {
                    debugger;
                    let user = rs.data.obj;
                    sessionStorage.setItem("user", JSON.stringify(user));
                    sessionStorage.setItem("showNum", 5);
                    sessionStorage.setItem("typeId", 0);
                    location.href = 'home.html';
                }).catch(e => {
                    layer.msg(e.message);
                })
            }
            // this.user=JSON.parse(sessionStorage.getItem("user"));
            // this.defaultAddrId=JSON.parse(sessionStorage.getItem("user")).defaultAddrid;
        }
    },
    mounted: function () {
        this.getUserInfo();
        this.getGoods();
        this.tatalMoney = this.goods.price;
        $(".totalMoney").html("$" + this.tatalMoney);
        this.isFavorate();
        this.getAllGoodsType();
    }
});